package cn.itcast.demo.service;

import cn.itcast.demo.mapper.UserMapper;
import cn.itcast.demo.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {


    //获取一个日志的工具
    Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;//mybatis提供的mapper

    //查询时候随机睡眠，1-2000 毫秒
    public User queryUserById(Long userId) {

        long startTime = System.currentTimeMillis();

        User user = this.userMapper.selectByPrimaryKey(userId);//调用通用mapper的查询方法

        try {
            Thread.sleep((int)(Math.random()*2000+1));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();


        //大括号为占位符
        logger.info("本次查询的id为：{},耗时为：{}毫秒",userId,endTime-startTime);
        return user;
    }

    @Transactional
    public void deleteById(Long id){
        this.userMapper.deleteByPrimaryKey(id);
    }
}
